


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SubSonic.DataProviders;
using SubSonic.Extensions;
using System.Linq.Expressions;
using SubSonic.Schema;
using System.Collections;
using SubSonic;
using SubSonic.Repository;
using System.ComponentModel;
using System.Data.Common;

namespace V5_EShop.Data
{
    
    
    /// <summary>
    /// A class which represents the Tbl_UserToArea table in the BookEShop Database.
    /// </summary>
    public partial class ModelUserToArea: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelUserToArea> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelUserToArea>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelUserToArea> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelUserToArea item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelUserToArea item=new ModelUserToArea();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelUserToArea> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelUserToArea(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelUserToArea.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelUserToArea>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelUserToArea(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelUserToArea(Expression<Func<ModelUserToArea, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelUserToArea> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelUserToArea> _repo;
            
            if(db.TestMode){
                ModelUserToArea.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelUserToArea>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelUserToArea> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelUserToArea SingleOrDefault(Expression<Func<ModelUserToArea, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelUserToArea single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelUserToArea SingleOrDefault(Expression<Func<ModelUserToArea, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelUserToArea single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelUserToArea, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelUserToArea, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelUserToArea> Find(Expression<Func<ModelUserToArea, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelUserToArea> Find(Expression<Func<ModelUserToArea, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelUserToArea> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelUserToArea> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelUserToArea> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelUserToArea> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelUserToArea> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelUserToArea> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "id";
        }

        public object KeyValue()
        {
            return this.id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.tousername.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelUserToArea)){
                ModelUserToArea compare=(ModelUserToArea)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.id;
        }
        
        public string DescriptorValue()
        {
                            return this.tousername.ToString();
                    }

        public string DescriptorColumn() {
            return "tousername";
        }
        public static string GetKeyColumn()
        {
            return "id";
        }        
        public static string GetDescriptorColumn()
        {
            return "tousername";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _id;
        public int id
        {
            get { return _id; }
            set
            {
                if(_id!=value){
                    _id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _tousername;
        public string tousername
        {
            get { return _tousername; }
            set
            {
                if(_tousername!=value){
                    _tousername=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="tousername");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _touserareapro;
        public string touserareapro
        {
            get { return _touserareapro; }
            set
            {
                if(_touserareapro!=value){
                    _touserareapro=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="touserareapro");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _touserareacity;
        public string touserareacity
        {
            get { return _touserareacity; }
            set
            {
                if(_touserareacity!=value){
                    _touserareacity=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="touserareacity");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _touserareacity2;
        public string touserareacity2
        {
            get { return _touserareacity2; }
            set
            {
                if(_touserareacity2!=value){
                    _touserareacity2=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="touserareacity2");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _touserurl;
        public string touserurl
        {
            get { return _touserurl; }
            set
            {
                if(_touserurl!=value){
                    _touserurl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="touserurl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _topostnum;
        public string topostnum
        {
            get { return _topostnum; }
            set
            {
                if(_topostnum!=value){
                    _topostnum=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="topostnum");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _touserphone;
        public string touserphone
        {
            get { return _touserphone; }
            set
            {
                if(_touserphone!=value){
                    _touserphone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="touserphone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _tousertelephone;
        public string tousertelephone
        {
            get { return _tousertelephone; }
            set
            {
                if(_tousertelephone!=value){
                    _tousertelephone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="tousertelephone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _adddatetime;
        public DateTime? adddatetime
        {
            get { return _adddatetime; }
            set
            {
                if(_adddatetime!=value){
                    _adddatetime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="adddatetime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _userid;
        public int? userid
        {
            get { return _userid; }
            set
            {
                if(_userid!=value){
                    _userid=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="userid");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelUserToArea, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_UserRole table in the BookEShop Database.
    /// </summary>
    public partial class ModelUserRole: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelUserRole> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelUserRole>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelUserRole> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelUserRole item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelUserRole item=new ModelUserRole();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelUserRole> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelUserRole(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelUserRole.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelUserRole>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelUserRole(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelUserRole(Expression<Func<ModelUserRole, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelUserRole> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelUserRole> _repo;
            
            if(db.TestMode){
                ModelUserRole.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelUserRole>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelUserRole> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelUserRole SingleOrDefault(Expression<Func<ModelUserRole, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelUserRole single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelUserRole SingleOrDefault(Expression<Func<ModelUserRole, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelUserRole single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelUserRole, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelUserRole, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelUserRole> Find(Expression<Func<ModelUserRole, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelUserRole> Find(Expression<Func<ModelUserRole, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelUserRole> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelUserRole> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelUserRole> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelUserRole> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelUserRole> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelUserRole> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.RoleName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelUserRole)){
                ModelUserRole compare=(ModelUserRole)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.RoleName.ToString();
                    }

        public string DescriptorColumn() {
            return "RoleName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "RoleName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _RoleName;
        public string RoleName
        {
            get { return _RoleName; }
            set
            {
                if(_RoleName!=value){
                    _RoleName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Discount;
        public string Discount
        {
            get { return _Discount; }
            set
            {
                if(_Discount!=value){
                    _Discount=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Discount");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelUserRole, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_B_BookClass table in the BookEShop Database.
    /// </summary>
    public partial class ModelB_BookClass: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelB_BookClass> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelB_BookClass>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelB_BookClass> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelB_BookClass item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelB_BookClass item=new ModelB_BookClass();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelB_BookClass> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelB_BookClass(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelB_BookClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookClass>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelB_BookClass(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelB_BookClass(Expression<Func<ModelB_BookClass, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelB_BookClass> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelB_BookClass> _repo;
            
            if(db.TestMode){
                ModelB_BookClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookClass>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelB_BookClass> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelB_BookClass SingleOrDefault(Expression<Func<ModelB_BookClass, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelB_BookClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelB_BookClass SingleOrDefault(Expression<Func<ModelB_BookClass, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelB_BookClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelB_BookClass, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelB_BookClass, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelB_BookClass> Find(Expression<Func<ModelB_BookClass, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelB_BookClass> Find(Expression<Func<ModelB_BookClass, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelB_BookClass> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelB_BookClass> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelB_BookClass> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelB_BookClass> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelB_BookClass> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelB_BookClass> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ClassID";
        }

        public object KeyValue()
        {
            return this.ClassID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ClassCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelB_BookClass)){
                ModelB_BookClass compare=(ModelB_BookClass)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ClassID;
        }
        
        public string DescriptorValue()
        {
                            return this.ClassCode.ToString();
                    }

        public string DescriptorColumn() {
            return "ClassCode";
        }
        public static string GetKeyColumn()
        {
            return "ClassID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ClassCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ClassID;
        public int ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ChannelID;
        public int? ChannelID
        {
            get { return _ChannelID; }
            set
            {
                if(_ChannelID!=value){
                    _ChannelID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ChannelID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassCode;
        public string ClassCode
        {
            get { return _ClassCode; }
            set
            {
                if(_ClassCode!=value){
                    _ClassCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassPhoto;
        public string ClassPhoto
        {
            get { return _ClassPhoto; }
            set
            {
                if(_ClassPhoto!=value){
                    _ClassPhoto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassPhoto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassKeyWord;
        public string ClassKeyWord
        {
            get { return _ClassKeyWord; }
            set
            {
                if(_ClassKeyWord!=value){
                    _ClassKeyWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassKeyWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassDescription;
        public string ClassDescription
        {
            get { return _ClassDescription; }
            set
            {
                if(_ClassDescription!=value){
                    _ClassDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassTitle;
        public string ClassTitle
        {
            get { return _ClassTitle; }
            set
            {
                if(_ClassTitle!=value){
                    _ClassTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ParentID;
        public int? ParentID
        {
            get { return _ParentID; }
            set
            {
                if(_ParentID!=value){
                    _ParentID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Info;
        public string Info
        {
            get { return _Info; }
            set
            {
                if(_Info!=value){
                    _Info=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Info");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelB_BookClass, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_User table in the BookEShop Database.
    /// </summary>
    public partial class ModelUser: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelUser> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelUser>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelUser> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelUser item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelUser item=new ModelUser();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelUser> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelUser(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelUser.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelUser>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelUser(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelUser(Expression<Func<ModelUser, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelUser> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelUser> _repo;
            
            if(db.TestMode){
                ModelUser.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelUser>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelUser> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelUser SingleOrDefault(Expression<Func<ModelUser, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelUser single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelUser SingleOrDefault(Expression<Func<ModelUser, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelUser single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelUser, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelUser, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelUser> Find(Expression<Func<ModelUser, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelUser> Find(Expression<Func<ModelUser, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelUser> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelUser> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelUser> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelUser> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelUser> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelUser> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.UserName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelUser)){
                ModelUser compare=(ModelUser)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.UserName.ToString();
                    }

        public string DescriptorColumn() {
            return "UserName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "UserName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UserName;
        public string UserName
        {
            get { return _UserName; }
            set
            {
                if(_UserName!=value){
                    _UserName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UserPassword;
        public string UserPassword
        {
            get { return _UserPassword; }
            set
            {
                if(_UserPassword!=value){
                    _UserPassword=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserPassword");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _RegDate;
        public DateTime? RegDate
        {
            get { return _RegDate; }
            set
            {
                if(_RegDate!=value){
                    _RegDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RegDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _UserScore;
        public int? UserScore
        {
            get { return _UserScore; }
            set
            {
                if(_UserScore!=value){
                    _UserScore=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserScore");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ScoreUsed;
        public int? ScoreUsed
        {
            get { return _ScoreUsed; }
            set
            {
                if(_ScoreUsed!=value){
                    _ScoreUsed=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ScoreUsed");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UserEmail;
        public string UserEmail
        {
            get { return _UserEmail; }
            set
            {
                if(_UserEmail!=value){
                    _UserEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserEmail");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _UserSex;
        public int? UserSex
        {
            get { return _UserSex; }
            set
            {
                if(_UserSex!=value){
                    _UserSex=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserSex");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LoginDate;
        public DateTime? LoginDate
        {
            get { return _LoginDate; }
            set
            {
                if(_LoginDate!=value){
                    _LoginDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LoginDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _LoginTimes;
        public int? LoginTimes
        {
            get { return _LoginTimes; }
            set
            {
                if(_LoginTimes!=value){
                    _LoginTimes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LoginTimes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _IdentityCard;
        public string IdentityCard
        {
            get { return _IdentityCard; }
            set
            {
                if(_IdentityCard!=value){
                    _IdentityCard=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IdentityCard");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _PostCode;
        public int? PostCode
        {
            get { return _PostCode; }
            set
            {
                if(_PostCode!=value){
                    _PostCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PostCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _AreaID;
        public int? AreaID
        {
            get { return _AreaID; }
            set
            {
                if(_AreaID!=value){
                    _AreaID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AreaID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Address3;
        public string Address3
        {
            get { return _Address3; }
            set
            {
                if(_Address3!=value){
                    _Address3=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Address3");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Address2;
        public string Address2
        {
            get { return _Address2; }
            set
            {
                if(_Address2!=value){
                    _Address2=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Address2");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Address;
        public string Address
        {
            get { return _Address; }
            set
            {
                if(_Address!=value){
                    _Address=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Address");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsLock;
        public int? IsLock
        {
            get { return _IsLock; }
            set
            {
                if(_IsLock!=value){
                    _IsLock=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsLock");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _GroupID;
        public int? GroupID
        {
            get { return _GroupID; }
            set
            {
                if(_GroupID!=value){
                    _GroupID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="GroupID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Question;
        public string Question
        {
            get { return _Question; }
            set
            {
                if(_Question!=value){
                    _Question=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Question");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Answer;
        public string Answer
        {
            get { return _Answer; }
            set
            {
                if(_Answer!=value){
                    _Answer=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Answer");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LoginIP;
        public string LoginIP
        {
            get { return _LoginIP; }
            set
            {
                if(_LoginIP!=value){
                    _LoginIP=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LoginIP");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        decimal? _FrozenDeposit;
        public decimal? FrozenDeposit
        {
            get { return _FrozenDeposit; }
            set
            {
                if(_FrozenDeposit!=value){
                    _FrozenDeposit=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FrozenDeposit");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _FrozenScore;
        public int? FrozenScore
        {
            get { return _FrozenScore; }
            set
            {
                if(_FrozenScore!=value){
                    _FrozenScore=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FrozenScore");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BirthDay;
        public string BirthDay
        {
            get { return _BirthDay; }
            set
            {
                if(_BirthDay!=value){
                    _BirthDay=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BirthDay");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _QQ;
        public string QQ
        {
            get { return _QQ; }
            set
            {
                if(_QQ!=value){
                    _QQ=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="QQ");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Telephone;
        public string Telephone
        {
            get { return _Telephone; }
            set
            {
                if(_Telephone!=value){
                    _Telephone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Telephone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Phone;
        public string Phone
        {
            get { return _Phone; }
            set
            {
                if(_Phone!=value){
                    _Phone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Phone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _TrueName;
        public string TrueName
        {
            get { return _TrueName; }
            set
            {
                if(_TrueName!=value){
                    _TrueName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TrueName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CommendUser;
        public string CommendUser
        {
            get { return _CommendUser; }
            set
            {
                if(_CommendUser!=value){
                    _CommendUser=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CommendUser");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _NewPassWord;
        public string NewPassWord
        {
            get { return _NewPassWord; }
            set
            {
                if(_NewPassWord!=value){
                    _NewPassWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="NewPassWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _GUID;
        public string GUID
        {
            get { return _GUID; }
            set
            {
                if(_GUID!=value){
                    _GUID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="GUID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Readme;
        public string Readme
        {
            get { return _Readme; }
            set
            {
                if(_Readme!=value){
                    _Readme=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Readme");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelUser, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_B_BookTopicClass table in the BookEShop Database.
    /// </summary>
    public partial class ModelB_BookTopicClass: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelB_BookTopicClass> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelB_BookTopicClass>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelB_BookTopicClass> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelB_BookTopicClass item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelB_BookTopicClass item=new ModelB_BookTopicClass();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelB_BookTopicClass> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelB_BookTopicClass(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelB_BookTopicClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookTopicClass>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelB_BookTopicClass(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelB_BookTopicClass(Expression<Func<ModelB_BookTopicClass, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelB_BookTopicClass> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelB_BookTopicClass> _repo;
            
            if(db.TestMode){
                ModelB_BookTopicClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookTopicClass>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelB_BookTopicClass> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelB_BookTopicClass SingleOrDefault(Expression<Func<ModelB_BookTopicClass, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelB_BookTopicClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelB_BookTopicClass SingleOrDefault(Expression<Func<ModelB_BookTopicClass, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelB_BookTopicClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelB_BookTopicClass, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelB_BookTopicClass, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelB_BookTopicClass> Find(Expression<Func<ModelB_BookTopicClass, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelB_BookTopicClass> Find(Expression<Func<ModelB_BookTopicClass, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelB_BookTopicClass> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelB_BookTopicClass> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelB_BookTopicClass> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelB_BookTopicClass> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelB_BookTopicClass> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelB_BookTopicClass> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ClassID";
        }

        public object KeyValue()
        {
            return this.ClassID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ClassCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelB_BookTopicClass)){
                ModelB_BookTopicClass compare=(ModelB_BookTopicClass)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ClassID;
        }
        
        public string DescriptorValue()
        {
                            return this.ClassCode.ToString();
                    }

        public string DescriptorColumn() {
            return "ClassCode";
        }
        public static string GetKeyColumn()
        {
            return "ClassID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ClassCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ClassID;
        public int ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ChannelID;
        public int? ChannelID
        {
            get { return _ChannelID; }
            set
            {
                if(_ChannelID!=value){
                    _ChannelID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ChannelID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassCode;
        public string ClassCode
        {
            get { return _ClassCode; }
            set
            {
                if(_ClassCode!=value){
                    _ClassCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassPhoto;
        public string ClassPhoto
        {
            get { return _ClassPhoto; }
            set
            {
                if(_ClassPhoto!=value){
                    _ClassPhoto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassPhoto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassKeyWord;
        public string ClassKeyWord
        {
            get { return _ClassKeyWord; }
            set
            {
                if(_ClassKeyWord!=value){
                    _ClassKeyWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassKeyWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassDescription;
        public string ClassDescription
        {
            get { return _ClassDescription; }
            set
            {
                if(_ClassDescription!=value){
                    _ClassDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassTitle;
        public string ClassTitle
        {
            get { return _ClassTitle; }
            set
            {
                if(_ClassTitle!=value){
                    _ClassTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ParentID;
        public int? ParentID
        {
            get { return _ParentID; }
            set
            {
                if(_ParentID!=value){
                    _ParentID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Info;
        public string Info
        {
            get { return _Info; }
            set
            {
                if(_Info!=value){
                    _Info=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Info");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelB_BookTopicClass, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Shop_Cart table in the BookEShop Database.
    /// </summary>
    public partial class ModelShop_Cart: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelShop_Cart> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelShop_Cart>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelShop_Cart> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelShop_Cart item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelShop_Cart item=new ModelShop_Cart();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelShop_Cart> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelShop_Cart(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelShop_Cart.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Cart>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelShop_Cart(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelShop_Cart(Expression<Func<ModelShop_Cart, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelShop_Cart> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelShop_Cart> _repo;
            
            if(db.TestMode){
                ModelShop_Cart.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Cart>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelShop_Cart> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelShop_Cart SingleOrDefault(Expression<Func<ModelShop_Cart, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelShop_Cart single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelShop_Cart SingleOrDefault(Expression<Func<ModelShop_Cart, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelShop_Cart single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelShop_Cart, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelShop_Cart, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelShop_Cart> Find(Expression<Func<ModelShop_Cart, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelShop_Cart> Find(Expression<Func<ModelShop_Cart, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelShop_Cart> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelShop_Cart> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelShop_Cart> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelShop_Cart> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelShop_Cart> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelShop_Cart> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ID.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelShop_Cart)){
                ModelShop_Cart compare=(ModelShop_Cart)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.ID.ToString();
                    }

        public string DescriptorColumn() {
            return "ID";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ID";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelShop_Cart, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_ArticleComment table in the BookEShop Database.
    /// </summary>
    public partial class ModelArticleComment: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelArticleComment> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelArticleComment>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelArticleComment> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelArticleComment item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelArticleComment item=new ModelArticleComment();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelArticleComment> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelArticleComment(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelArticleComment.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelArticleComment>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelArticleComment(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelArticleComment(Expression<Func<ModelArticleComment, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelArticleComment> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelArticleComment> _repo;
            
            if(db.TestMode){
                ModelArticleComment.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelArticleComment>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelArticleComment> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelArticleComment SingleOrDefault(Expression<Func<ModelArticleComment, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelArticleComment single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelArticleComment SingleOrDefault(Expression<Func<ModelArticleComment, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelArticleComment single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelArticleComment, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelArticleComment, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelArticleComment> Find(Expression<Func<ModelArticleComment, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelArticleComment> Find(Expression<Func<ModelArticleComment, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelArticleComment> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelArticleComment> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelArticleComment> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelArticleComment> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelArticleComment> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelArticleComment> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.CommentName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelArticleComment)){
                ModelArticleComment compare=(ModelArticleComment)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.CommentName.ToString();
                    }

        public string DescriptorColumn() {
            return "CommentName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "CommentName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CommentName;
        public string CommentName
        {
            get { return _CommentName; }
            set
            {
                if(_CommentName!=value){
                    _CommentName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CommentName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CommentContent;
        public string CommentContent
        {
            get { return _CommentContent; }
            set
            {
                if(_CommentContent!=value){
                    _CommentContent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CommentContent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ClassID;
        public int? ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SelfID;
        public int? SelfID
        {
            get { return _SelfID; }
            set
            {
                if(_SelfID!=value){
                    _SelfID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SelfID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _AddDateTime;
        public DateTime? AddDateTime
        {
            get { return _AddDateTime; }
            set
            {
                if(_AddDateTime!=value){
                    _AddDateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AddDateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReplayContent;
        public string ReplayContent
        {
            get { return _ReplayContent; }
            set
            {
                if(_ReplayContent!=value){
                    _ReplayContent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReplayContent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ReplayTime;
        public DateTime? ReplayTime
        {
            get { return _ReplayTime; }
            set
            {
                if(_ReplayTime!=value){
                    _ReplayTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReplayTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReplayName;
        public string ReplayName
        {
            get { return _ReplayName; }
            set
            {
                if(_ReplayName!=value){
                    _ReplayName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReplayName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CommentIP;
        public string CommentIP
        {
            get { return _CommentIP; }
            set
            {
                if(_CommentIP!=value){
                    _CommentIP=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CommentIP");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelArticleComment, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_B_BookComment table in the BookEShop Database.
    /// </summary>
    public partial class ModelB_BookComment: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelB_BookComment> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelB_BookComment>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelB_BookComment> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelB_BookComment item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelB_BookComment item=new ModelB_BookComment();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelB_BookComment> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelB_BookComment(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelB_BookComment.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookComment>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelB_BookComment(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelB_BookComment(Expression<Func<ModelB_BookComment, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelB_BookComment> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelB_BookComment> _repo;
            
            if(db.TestMode){
                ModelB_BookComment.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookComment>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelB_BookComment> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelB_BookComment SingleOrDefault(Expression<Func<ModelB_BookComment, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelB_BookComment single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelB_BookComment SingleOrDefault(Expression<Func<ModelB_BookComment, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelB_BookComment single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelB_BookComment, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelB_BookComment, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelB_BookComment> Find(Expression<Func<ModelB_BookComment, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelB_BookComment> Find(Expression<Func<ModelB_BookComment, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelB_BookComment> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelB_BookComment> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelB_BookComment> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelB_BookComment> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelB_BookComment> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelB_BookComment> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.CommentName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelB_BookComment)){
                ModelB_BookComment compare=(ModelB_BookComment)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.CommentName.ToString();
                    }

        public string DescriptorColumn() {
            return "CommentName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "CommentName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CommentName;
        public string CommentName
        {
            get { return _CommentName; }
            set
            {
                if(_CommentName!=value){
                    _CommentName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CommentName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CommentContent;
        public string CommentContent
        {
            get { return _CommentContent; }
            set
            {
                if(_CommentContent!=value){
                    _CommentContent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CommentContent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ClassID;
        public int? ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SelfID;
        public int? SelfID
        {
            get { return _SelfID; }
            set
            {
                if(_SelfID!=value){
                    _SelfID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SelfID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _AddDateTime;
        public DateTime? AddDateTime
        {
            get { return _AddDateTime; }
            set
            {
                if(_AddDateTime!=value){
                    _AddDateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AddDateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReplayContent;
        public string ReplayContent
        {
            get { return _ReplayContent; }
            set
            {
                if(_ReplayContent!=value){
                    _ReplayContent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReplayContent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _ReplayTime;
        public DateTime? ReplayTime
        {
            get { return _ReplayTime; }
            set
            {
                if(_ReplayTime!=value){
                    _ReplayTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReplayTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReplayName;
        public string ReplayName
        {
            get { return _ReplayName; }
            set
            {
                if(_ReplayName!=value){
                    _ReplayName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReplayName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CommentIP;
        public string CommentIP
        {
            get { return _CommentIP; }
            set
            {
                if(_CommentIP!=value){
                    _CommentIP=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CommentIP");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelB_BookComment, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Shop_Payment table in the BookEShop Database.
    /// </summary>
    public partial class ModelShop_Payment: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelShop_Payment> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelShop_Payment>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelShop_Payment> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelShop_Payment item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelShop_Payment item=new ModelShop_Payment();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelShop_Payment> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelShop_Payment(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelShop_Payment.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Payment>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelShop_Payment(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelShop_Payment(Expression<Func<ModelShop_Payment, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelShop_Payment> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelShop_Payment> _repo;
            
            if(db.TestMode){
                ModelShop_Payment.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Payment>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelShop_Payment> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelShop_Payment SingleOrDefault(Expression<Func<ModelShop_Payment, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelShop_Payment single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelShop_Payment SingleOrDefault(Expression<Func<ModelShop_Payment, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelShop_Payment single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelShop_Payment, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelShop_Payment, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelShop_Payment> Find(Expression<Func<ModelShop_Payment, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelShop_Payment> Find(Expression<Func<ModelShop_Payment, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelShop_Payment> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelShop_Payment> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelShop_Payment> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelShop_Payment> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelShop_Payment> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelShop_Payment> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.PayName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelShop_Payment)){
                ModelShop_Payment compare=(ModelShop_Payment)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.PayName.ToString();
                    }

        public string DescriptorColumn() {
            return "PayName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "PayName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PayName;
        public string PayName
        {
            get { return _PayName; }
            set
            {
                if(_PayName!=value){
                    _PayName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PayName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _PayType;
        public int? PayType
        {
            get { return _PayType; }
            set
            {
                if(_PayType!=value){
                    _PayType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PayType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Interface;
        public int? Interface
        {
            get { return _Interface; }
            set
            {
                if(_Interface!=value){
                    _Interface=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Interface");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UserID;
        public string UserID
        {
            get { return _UserID; }
            set
            {
                if(_UserID!=value){
                    _UserID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PrivateKey;
        public string PrivateKey
        {
            get { return _PrivateKey; }
            set
            {
                if(_PrivateKey!=value){
                    _PrivateKey=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PrivateKey");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ParentID;
        public int? ParentID
        {
            get { return _ParentID; }
            set
            {
                if(_ParentID!=value){
                    _ParentID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Email;
        public string Email
        {
            get { return _Email; }
            set
            {
                if(_Email!=value){
                    _Email=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Email");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _AliPaySetting;
        public string AliPaySetting
        {
            get { return _AliPaySetting; }
            set
            {
                if(_AliPaySetting!=value){
                    _AliPaySetting=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AliPaySetting");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SupportCoins;
        public string SupportCoins
        {
            get { return _SupportCoins; }
            set
            {
                if(_SupportCoins!=value){
                    _SupportCoins=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SupportCoins");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Content;
        public string Content
        {
            get { return _Content; }
            set
            {
                if(_Content!=value){
                    _Content=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Content");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Status;
        public int? Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelShop_Payment, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Shop_Shipment table in the BookEShop Database.
    /// </summary>
    public partial class ModelShop_Shipment: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelShop_Shipment> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelShop_Shipment>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelShop_Shipment> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelShop_Shipment item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelShop_Shipment item=new ModelShop_Shipment();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelShop_Shipment> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelShop_Shipment(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelShop_Shipment.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Shipment>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelShop_Shipment(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelShop_Shipment(Expression<Func<ModelShop_Shipment, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelShop_Shipment> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelShop_Shipment> _repo;
            
            if(db.TestMode){
                ModelShop_Shipment.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Shipment>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelShop_Shipment> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelShop_Shipment SingleOrDefault(Expression<Func<ModelShop_Shipment, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelShop_Shipment single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelShop_Shipment SingleOrDefault(Expression<Func<ModelShop_Shipment, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelShop_Shipment single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelShop_Shipment, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelShop_Shipment, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelShop_Shipment> Find(Expression<Func<ModelShop_Shipment, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelShop_Shipment> Find(Expression<Func<ModelShop_Shipment, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelShop_Shipment> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelShop_Shipment> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelShop_Shipment> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelShop_Shipment> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelShop_Shipment> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelShop_Shipment> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ShipName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelShop_Shipment)){
                ModelShop_Shipment compare=(ModelShop_Shipment)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.ShipName.ToString();
                    }

        public string DescriptorColumn() {
            return "ShipName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ShipName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ShipName;
        public string ShipName
        {
            get { return _ShipName; }
            set
            {
                if(_ShipName!=value){
                    _ShipName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Content;
        public string Content
        {
            get { return _Content; }
            set
            {
                if(_Content!=value){
                    _Content=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Content");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Status;
        public int? Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelShop_Shipment, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Shop_Order table in the BookEShop Database.
    /// </summary>
    public partial class ModelShop_Order: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelShop_Order> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelShop_Order>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelShop_Order> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelShop_Order item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelShop_Order item=new ModelShop_Order();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelShop_Order> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelShop_Order(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelShop_Order.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Order>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelShop_Order(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelShop_Order(Expression<Func<ModelShop_Order, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelShop_Order> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelShop_Order> _repo;
            
            if(db.TestMode){
                ModelShop_Order.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Order>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelShop_Order> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelShop_Order SingleOrDefault(Expression<Func<ModelShop_Order, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelShop_Order single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelShop_Order SingleOrDefault(Expression<Func<ModelShop_Order, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelShop_Order single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelShop_Order, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelShop_Order, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelShop_Order> Find(Expression<Func<ModelShop_Order, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelShop_Order> Find(Expression<Func<ModelShop_Order, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelShop_Order> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelShop_Order> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelShop_Order> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelShop_Order> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelShop_Order> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelShop_Order> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "OrderID";
        }

        public object KeyValue()
        {
            return this.OrderID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.OrderNo.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelShop_Order)){
                ModelShop_Order compare=(ModelShop_Order)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.OrderID;
        }
        
        public string DescriptorValue()
        {
                            return this.OrderNo.ToString();
                    }

        public string DescriptorColumn() {
            return "OrderNo";
        }
        public static string GetKeyColumn()
        {
            return "OrderID";
        }        
        public static string GetDescriptorColumn()
        {
            return "OrderNo";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _OrderID;
        public int OrderID
        {
            get { return _OrderID; }
            set
            {
                if(_OrderID!=value){
                    _OrderID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _MemberID;
        public int? MemberID
        {
            get { return _MemberID; }
            set
            {
                if(_MemberID!=value){
                    _MemberID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MemberID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _OrderNo;
        public string OrderNo
        {
            get { return _OrderNo; }
            set
            {
                if(_OrderNo!=value){
                    _OrderNo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderNo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _OrderTime;
        public DateTime? OrderTime
        {
            get { return _OrderTime; }
            set
            {
                if(_OrderTime!=value){
                    _OrderTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BuyerName;
        public string BuyerName
        {
            get { return _BuyerName; }
            set
            {
                if(_BuyerName!=value){
                    _BuyerName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BuyerName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BuyerPhone;
        public string BuyerPhone
        {
            get { return _BuyerPhone; }
            set
            {
                if(_BuyerPhone!=value){
                    _BuyerPhone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BuyerPhone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BuyerEmail;
        public string BuyerEmail
        {
            get { return _BuyerEmail; }
            set
            {
                if(_BuyerEmail!=value){
                    _BuyerEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BuyerEmail");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BuyerAddress;
        public string BuyerAddress
        {
            get { return _BuyerAddress; }
            set
            {
                if(_BuyerAddress!=value){
                    _BuyerAddress=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BuyerAddress");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BuyerPostalcode;
        public string BuyerPostalcode
        {
            get { return _BuyerPostalcode; }
            set
            {
                if(_BuyerPostalcode!=value){
                    _BuyerPostalcode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BuyerPostalcode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReceiverName;
        public string ReceiverName
        {
            get { return _ReceiverName; }
            set
            {
                if(_ReceiverName!=value){
                    _ReceiverName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReceiverName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReceiverPhone;
        public string ReceiverPhone
        {
            get { return _ReceiverPhone; }
            set
            {
                if(_ReceiverPhone!=value){
                    _ReceiverPhone=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReceiverPhone");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReceiverEmail;
        public string ReceiverEmail
        {
            get { return _ReceiverEmail; }
            set
            {
                if(_ReceiverEmail!=value){
                    _ReceiverEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReceiverEmail");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReceiverProCities;
        public string ReceiverProCities
        {
            get { return _ReceiverProCities; }
            set
            {
                if(_ReceiverProCities!=value){
                    _ReceiverProCities=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReceiverProCities");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReceiverAddress;
        public string ReceiverAddress
        {
            get { return _ReceiverAddress; }
            set
            {
                if(_ReceiverAddress!=value){
                    _ReceiverAddress=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReceiverAddress");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ReceiverPostalcode;
        public string ReceiverPostalcode
        {
            get { return _ReceiverPostalcode; }
            set
            {
                if(_ReceiverPostalcode!=value){
                    _ReceiverPostalcode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReceiverPostalcode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        decimal? _TotalPrice;
        public decimal? TotalPrice
        {
            get { return _TotalPrice; }
            set
            {
                if(_TotalPrice!=value){
                    _TotalPrice=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TotalPrice");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Discount;
        public string Discount
        {
            get { return _Discount; }
            set
            {
                if(_Discount!=value){
                    _Discount=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Discount");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        decimal? _ShipPrice;
        public decimal? ShipPrice
        {
            get { return _ShipPrice; }
            set
            {
                if(_ShipPrice!=value){
                    _ShipPrice=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipPrice");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PaymentType;
        public string PaymentType
        {
            get { return _PaymentType; }
            set
            {
                if(_PaymentType!=value){
                    _PaymentType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PaymentType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ShippingType;
        public string ShippingType
        {
            get { return _ShippingType; }
            set
            {
                if(_ShippingType!=value){
                    _ShippingType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShippingType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Score;
        public int? Score
        {
            get { return _Score; }
            set
            {
                if(_Score!=value){
                    _Score=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Score");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        byte? _NeedInvoice;
        public byte? NeedInvoice
        {
            get { return _NeedInvoice; }
            set
            {
                if(_NeedInvoice!=value){
                    _NeedInvoice=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="NeedInvoice");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _InvoiceHead;
        public string InvoiceHead
        {
            get { return _InvoiceHead; }
            set
            {
                if(_InvoiceHead!=value){
                    _InvoiceHead=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="InvoiceHead");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _InvoiceContent;
        public string InvoiceContent
        {
            get { return _InvoiceContent; }
            set
            {
                if(_InvoiceContent!=value){
                    _InvoiceContent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="InvoiceContent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _PayStatus;
        public int? PayStatus
        {
            get { return _PayStatus; }
            set
            {
                if(_PayStatus!=value){
                    _PayStatus=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PayStatus");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ShippingStatus;
        public int? ShippingStatus
        {
            get { return _ShippingStatus; }
            set
            {
                if(_ShippingStatus!=value){
                    _ShippingStatus=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShippingStatus");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        byte? _IsCancelled;
        public byte? IsCancelled
        {
            get { return _IsCancelled; }
            set
            {
                if(_IsCancelled!=value){
                    _IsCancelled=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsCancelled");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        byte? _IsConfirmed;
        public byte? IsConfirmed
        {
            get { return _IsConfirmed; }
            set
            {
                if(_IsConfirmed!=value){
                    _IsConfirmed=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsConfirmed");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        byte? _IsFinished;
        public byte? IsFinished
        {
            get { return _IsFinished; }
            set
            {
                if(_IsFinished!=value){
                    _IsFinished=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsFinished");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _OrderStatus;
        public int? OrderStatus
        {
            get { return _OrderStatus; }
            set
            {
                if(_OrderStatus!=value){
                    _OrderStatus=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderStatus");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Remark;
        public string Remark
        {
            get { return _Remark; }
            set
            {
                if(_Remark!=value){
                    _Remark=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Remark");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelShop_Order, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Shop_OrderProduct table in the BookEShop Database.
    /// </summary>
    public partial class ModelShop_OrderProduct: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelShop_OrderProduct> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelShop_OrderProduct>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelShop_OrderProduct> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelShop_OrderProduct item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelShop_OrderProduct item=new ModelShop_OrderProduct();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelShop_OrderProduct> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelShop_OrderProduct(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelShop_OrderProduct.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_OrderProduct>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelShop_OrderProduct(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelShop_OrderProduct(Expression<Func<ModelShop_OrderProduct, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelShop_OrderProduct> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelShop_OrderProduct> _repo;
            
            if(db.TestMode){
                ModelShop_OrderProduct.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_OrderProduct>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelShop_OrderProduct> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelShop_OrderProduct SingleOrDefault(Expression<Func<ModelShop_OrderProduct, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelShop_OrderProduct single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelShop_OrderProduct SingleOrDefault(Expression<Func<ModelShop_OrderProduct, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelShop_OrderProduct single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelShop_OrderProduct, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelShop_OrderProduct, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelShop_OrderProduct> Find(Expression<Func<ModelShop_OrderProduct, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelShop_OrderProduct> Find(Expression<Func<ModelShop_OrderProduct, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelShop_OrderProduct> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelShop_OrderProduct> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelShop_OrderProduct> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelShop_OrderProduct> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelShop_OrderProduct> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelShop_OrderProduct> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ProductName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelShop_OrderProduct)){
                ModelShop_OrderProduct compare=(ModelShop_OrderProduct)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.ProductName.ToString();
                    }

        public string DescriptorColumn() {
            return "ProductName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ProductName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ProductName;
        public string ProductName
        {
            get { return _ProductName; }
            set
            {
                if(_ProductName!=value){
                    _ProductName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ProductName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ProductID;
        public int? ProductID
        {
            get { return _ProductID; }
            set
            {
                if(_ProductID!=value){
                    _ProductID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ProductID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _OrderNo;
        public string OrderNo
        {
            get { return _OrderNo; }
            set
            {
                if(_OrderNo!=value){
                    _OrderNo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderNo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Quantity;
        public int? Quantity
        {
            get { return _Quantity; }
            set
            {
                if(_Quantity!=value){
                    _Quantity=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Quantity");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        decimal? _ProductPrice;
        public decimal? ProductPrice
        {
            get { return _ProductPrice; }
            set
            {
                if(_ProductPrice!=value){
                    _ProductPrice=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ProductPrice");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        decimal? _TotalPrice;
        public decimal? TotalPrice
        {
            get { return _TotalPrice; }
            set
            {
                if(_TotalPrice!=value){
                    _TotalPrice=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TotalPrice");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Specification;
        public string Specification
        {
            get { return _Specification; }
            set
            {
                if(_Specification!=value){
                    _Specification=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Specification");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelShop_OrderProduct, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_B_Book table in the BookEShop Database.
    /// </summary>
    public partial class ModelB_Book: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelB_Book> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelB_Book>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelB_Book> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelB_Book item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelB_Book item=new ModelB_Book();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelB_Book> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelB_Book(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelB_Book.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_Book>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelB_Book(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelB_Book(Expression<Func<ModelB_Book, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelB_Book> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelB_Book> _repo;
            
            if(db.TestMode){
                ModelB_Book.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_Book>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelB_Book> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelB_Book SingleOrDefault(Expression<Func<ModelB_Book, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelB_Book single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelB_Book SingleOrDefault(Expression<Func<ModelB_Book, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelB_Book single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelB_Book, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelB_Book, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelB_Book> Find(Expression<Func<ModelB_Book, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelB_Book> Find(Expression<Func<ModelB_Book, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelB_Book> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelB_Book> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelB_Book> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelB_Book> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelB_Book> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelB_Book> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Isbn.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelB_Book)){
                ModelB_Book compare=(ModelB_Book)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.Isbn.ToString();
                    }

        public string DescriptorColumn() {
            return "Isbn";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Isbn";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ClassId;
        public int? ClassId
        {
            get { return _ClassId; }
            set
            {
                if(_ClassId!=value){
                    _ClassId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _BookId;
        public int? BookId
        {
            get { return _BookId; }
            set
            {
                if(_BookId!=value){
                    _BookId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BookId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Isbn;
        public string Isbn
        {
            get { return _Isbn; }
            set
            {
                if(_Isbn!=value){
                    _Isbn=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Isbn");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BookName;
        public string BookName
        {
            get { return _BookName; }
            set
            {
                if(_BookName!=value){
                    _BookName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BookName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _PubId;
        public int? PubId
        {
            get { return _PubId; }
            set
            {
                if(_PubId!=value){
                    _PubId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PubId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        decimal? _Price;
        public decimal? Price
        {
            get { return _Price; }
            set
            {
                if(_Price!=value){
                    _Price=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Price");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        decimal? _SellPrice;
        public decimal? SellPrice
        {
            get { return _SellPrice; }
            set
            {
                if(_SellPrice!=value){
                    _SellPrice=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SellPrice");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _PubDate;
        public DateTime? PubDate
        {
            get { return _PubDate; }
            set
            {
                if(_PubDate!=value){
                    _PubDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PubDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Ifhot;
        public int? Ifhot
        {
            get { return _Ifhot; }
            set
            {
                if(_Ifhot!=value){
                    _Ifhot=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Ifhot");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _RecDate;
        public DateTime? RecDate
        {
            get { return _RecDate; }
            set
            {
                if(_RecDate!=value){
                    _RecDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RecDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Jhzk;
        public string Jhzk
        {
            get { return _Jhzk; }
            set
            {
                if(_Jhzk!=value){
                    _Jhzk=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Jhzk");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Pxzk;
        public string Pxzk
        {
            get { return _Pxzk; }
            set
            {
                if(_Pxzk!=value){
                    _Pxzk=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Pxzk");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Lszk;
        public string Lszk
        {
            get { return _Lszk; }
            set
            {
                if(_Lszk!=value){
                    _Lszk=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Lszk");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Author;
        public string Author
        {
            get { return _Author; }
            set
            {
                if(_Author!=value){
                    _Author=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Author");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ZiShu;
        public int? ZiShu
        {
            get { return _ZiShu; }
            set
            {
                if(_ZiShu!=value){
                    _ZiShu=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ZiShu");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Zhzhen;
        public string Zhzhen
        {
            get { return _Zhzhen; }
            set
            {
                if(_Zhzhen!=value){
                    _Zhzhen=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Zhzhen");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _KaiBen;
        public string KaiBen
        {
            get { return _KaiBen; }
            set
            {
                if(_KaiBen!=value){
                    _KaiBen=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="KaiBen");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _KcMax;
        public int? KcMax
        {
            get { return _KcMax; }
            set
            {
                if(_KcMax!=value){
                    _KcMax=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="KcMax");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _KcMin;
        public int? KcMin
        {
            get { return _KcMin; }
            set
            {
                if(_KcMin!=value){
                    _KcMin=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="KcMin");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _YinCi;
        public int? YinCi
        {
            get { return _YinCi; }
            set
            {
                if(_YinCi!=value){
                    _YinCi=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="YinCi");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsbnKind;
        public int? IsbnKind
        {
            get { return _IsbnKind; }
            set
            {
                if(_IsbnKind!=value){
                    _IsbnKind=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsbnKind");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _WeiZhi;
        public string WeiZhi
        {
            get { return _WeiZhi; }
            set
            {
                if(_WeiZhi!=value){
                    _WeiZhi=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="WeiZhi");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _YinZhang;
        public string YinZhang
        {
            get { return _YinZhang; }
            set
            {
                if(_YinZhang!=value){
                    _YinZhang=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="YinZhang");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Note;
        public string Note
        {
            get { return _Note; }
            set
            {
                if(_Note!=value){
                    _Note=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Note");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _UpdateTime;
        public DateTime? UpdateTime
        {
            get { return _UpdateTime; }
            set
            {
                if(_UpdateTime!=value){
                    _UpdateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UpdateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelB_Book, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Friend table in the BookEShop Database.
    /// </summary>
    public partial class ModelFriend: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelFriend> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelFriend>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelFriend> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelFriend item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelFriend item=new ModelFriend();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelFriend> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelFriend(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelFriend.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelFriend>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelFriend(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelFriend(Expression<Func<ModelFriend, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelFriend> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelFriend> _repo;
            
            if(db.TestMode){
                ModelFriend.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelFriend>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelFriend> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelFriend SingleOrDefault(Expression<Func<ModelFriend, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelFriend single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelFriend SingleOrDefault(Expression<Func<ModelFriend, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelFriend single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelFriend, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelFriend, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelFriend> Find(Expression<Func<ModelFriend, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelFriend> Find(Expression<Func<ModelFriend, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelFriend> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelFriend> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelFriend> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelFriend> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelFriend> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelFriend> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.FriendTitle.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelFriend)){
                ModelFriend compare=(ModelFriend)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.FriendTitle.ToString();
                    }

        public string DescriptorColumn() {
            return "FriendTitle";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "FriendTitle";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ClassId;
        public int? ClassId
        {
            get { return _ClassId; }
            set
            {
                if(_ClassId!=value){
                    _ClassId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _LinkType;
        public int? LinkType
        {
            get { return _LinkType; }
            set
            {
                if(_LinkType!=value){
                    _LinkType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LinkType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FriendTitle;
        public string FriendTitle
        {
            get { return _FriendTitle; }
            set
            {
                if(_FriendTitle!=value){
                    _FriendTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FriendTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FriendUrl;
        public string FriendUrl
        {
            get { return _FriendUrl; }
            set
            {
                if(_FriendUrl!=value){
                    _FriendUrl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FriendUrl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FriendLogo;
        public string FriendLogo
        {
            get { return _FriendLogo; }
            set
            {
                if(_FriendLogo!=value){
                    _FriendLogo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FriendLogo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _FriendReadme;
        public string FriendReadme
        {
            get { return _FriendReadme; }
            set
            {
                if(_FriendReadme!=value){
                    _FriendReadme=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FriendReadme");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelFriend, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_HotKeyWord table in the BookEShop Database.
    /// </summary>
    public partial class ModelHotKeyWord: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelHotKeyWord> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelHotKeyWord>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelHotKeyWord> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelHotKeyWord item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelHotKeyWord item=new ModelHotKeyWord();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelHotKeyWord> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelHotKeyWord(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelHotKeyWord.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelHotKeyWord>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelHotKeyWord(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelHotKeyWord(Expression<Func<ModelHotKeyWord, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelHotKeyWord> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelHotKeyWord> _repo;
            
            if(db.TestMode){
                ModelHotKeyWord.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelHotKeyWord>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelHotKeyWord> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelHotKeyWord SingleOrDefault(Expression<Func<ModelHotKeyWord, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelHotKeyWord single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelHotKeyWord SingleOrDefault(Expression<Func<ModelHotKeyWord, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelHotKeyWord single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelHotKeyWord, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelHotKeyWord, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelHotKeyWord> Find(Expression<Func<ModelHotKeyWord, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelHotKeyWord> Find(Expression<Func<ModelHotKeyWord, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelHotKeyWord> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelHotKeyWord> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelHotKeyWord> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelHotKeyWord> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelHotKeyWord> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelHotKeyWord> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.HotKeyName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelHotKeyWord)){
                ModelHotKeyWord compare=(ModelHotKeyWord)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.HotKeyName.ToString();
                    }

        public string DescriptorColumn() {
            return "HotKeyName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "HotKeyName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ClassId;
        public int? ClassId
        {
            get { return _ClassId; }
            set
            {
                if(_ClassId!=value){
                    _ClassId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _HotKeyName;
        public string HotKeyName
        {
            get { return _HotKeyName; }
            set
            {
                if(_HotKeyName!=value){
                    _HotKeyName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="HotKeyName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _HotKeyUrl;
        public string HotKeyUrl
        {
            get { return _HotKeyUrl; }
            set
            {
                if(_HotKeyUrl!=value){
                    _HotKeyUrl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="HotKeyUrl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _OrderID;
        public int? OrderID
        {
            get { return _OrderID; }
            set
            {
                if(_OrderID!=value){
                    _OrderID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelHotKeyWord, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_HelpClass table in the BookEShop Database.
    /// </summary>
    public partial class ModelHelpClass: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelHelpClass> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelHelpClass>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelHelpClass> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelHelpClass item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelHelpClass item=new ModelHelpClass();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelHelpClass> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelHelpClass(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelHelpClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelHelpClass>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelHelpClass(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelHelpClass(Expression<Func<ModelHelpClass, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelHelpClass> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelHelpClass> _repo;
            
            if(db.TestMode){
                ModelHelpClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelHelpClass>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelHelpClass> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelHelpClass SingleOrDefault(Expression<Func<ModelHelpClass, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelHelpClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelHelpClass SingleOrDefault(Expression<Func<ModelHelpClass, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelHelpClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelHelpClass, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelHelpClass, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelHelpClass> Find(Expression<Func<ModelHelpClass, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelHelpClass> Find(Expression<Func<ModelHelpClass, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelHelpClass> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelHelpClass> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelHelpClass> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelHelpClass> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelHelpClass> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelHelpClass> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ClassID";
        }

        public object KeyValue()
        {
            return this.ClassID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ClassCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelHelpClass)){
                ModelHelpClass compare=(ModelHelpClass)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ClassID;
        }
        
        public string DescriptorValue()
        {
                            return this.ClassCode.ToString();
                    }

        public string DescriptorColumn() {
            return "ClassCode";
        }
        public static string GetKeyColumn()
        {
            return "ClassID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ClassCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ClassID;
        public int ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ChannelID;
        public int? ChannelID
        {
            get { return _ChannelID; }
            set
            {
                if(_ChannelID!=value){
                    _ChannelID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ChannelID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassCode;
        public string ClassCode
        {
            get { return _ClassCode; }
            set
            {
                if(_ClassCode!=value){
                    _ClassCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassPhoto;
        public string ClassPhoto
        {
            get { return _ClassPhoto; }
            set
            {
                if(_ClassPhoto!=value){
                    _ClassPhoto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassPhoto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassKeyWord;
        public string ClassKeyWord
        {
            get { return _ClassKeyWord; }
            set
            {
                if(_ClassKeyWord!=value){
                    _ClassKeyWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassKeyWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassDescription;
        public string ClassDescription
        {
            get { return _ClassDescription; }
            set
            {
                if(_ClassDescription!=value){
                    _ClassDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassTitle;
        public string ClassTitle
        {
            get { return _ClassTitle; }
            set
            {
                if(_ClassTitle!=value){
                    _ClassTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ParentID;
        public int? ParentID
        {
            get { return _ParentID; }
            set
            {
                if(_ParentID!=value){
                    _ParentID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Info;
        public string Info
        {
            get { return _Info; }
            set
            {
                if(_Info!=value){
                    _Info=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Info");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelHelpClass, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Help table in the BookEShop Database.
    /// </summary>
    public partial class ModelHelp: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelHelp> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelHelp>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelHelp> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelHelp item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelHelp item=new ModelHelp();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelHelp> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelHelp(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelHelp.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelHelp>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelHelp(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelHelp(Expression<Func<ModelHelp, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelHelp> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelHelp> _repo;
            
            if(db.TestMode){
                ModelHelp.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelHelp>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelHelp> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelHelp SingleOrDefault(Expression<Func<ModelHelp, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelHelp single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelHelp SingleOrDefault(Expression<Func<ModelHelp, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelHelp single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelHelp, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelHelp, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelHelp> Find(Expression<Func<ModelHelp, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelHelp> Find(Expression<Func<ModelHelp, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelHelp> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelHelp> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelHelp> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelHelp> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelHelp> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelHelp> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Title.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelHelp)){
                ModelHelp compare=(ModelHelp)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.Title.ToString();
                    }

        public string DescriptorColumn() {
            return "Title";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Title";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ClassID;
        public int? ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                if(_Title!=value){
                    _Title=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Title");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Content;
        public string Content
        {
            get { return _Content; }
            set
            {
                if(_Content!=value){
                    _Content=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Content");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _AddDateTime;
        public DateTime? AddDateTime
        {
            get { return _AddDateTime; }
            set
            {
                if(_AddDateTime!=value){
                    _AddDateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AddDateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Hits;
        public int? Hits
        {
            get { return _Hits; }
            set
            {
                if(_Hits!=value){
                    _Hits=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Hits");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelHelp, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_B_BookPublish table in the BookEShop Database.
    /// </summary>
    public partial class ModelB_BookPublish: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelB_BookPublish> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelB_BookPublish>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelB_BookPublish> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelB_BookPublish item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelB_BookPublish item=new ModelB_BookPublish();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelB_BookPublish> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelB_BookPublish(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelB_BookPublish.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookPublish>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelB_BookPublish(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelB_BookPublish(Expression<Func<ModelB_BookPublish, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelB_BookPublish> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelB_BookPublish> _repo;
            
            if(db.TestMode){
                ModelB_BookPublish.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookPublish>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelB_BookPublish> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelB_BookPublish SingleOrDefault(Expression<Func<ModelB_BookPublish, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelB_BookPublish single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelB_BookPublish SingleOrDefault(Expression<Func<ModelB_BookPublish, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelB_BookPublish single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelB_BookPublish, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelB_BookPublish, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelB_BookPublish> Find(Expression<Func<ModelB_BookPublish, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelB_BookPublish> Find(Expression<Func<ModelB_BookPublish, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelB_BookPublish> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelB_BookPublish> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelB_BookPublish> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelB_BookPublish> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelB_BookPublish> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelB_BookPublish> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.PubCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelB_BookPublish)){
                ModelB_BookPublish compare=(ModelB_BookPublish)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.PubCode.ToString();
                    }

        public string DescriptorColumn() {
            return "PubCode";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "PubCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _PubId;
        public int? PubId
        {
            get { return _PubId; }
            set
            {
                if(_PubId!=value){
                    _PubId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PubId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PubCode;
        public string PubCode
        {
            get { return _PubCode; }
            set
            {
                if(_PubCode!=value){
                    _PubCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PubCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PubPhoto;
        public string PubPhoto
        {
            get { return _PubPhoto; }
            set
            {
                if(_PubPhoto!=value){
                    _PubPhoto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PubPhoto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PubName;
        public string PubName
        {
            get { return _PubName; }
            set
            {
                if(_PubName!=value){
                    _PubName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PubName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _PubTel;
        public string PubTel
        {
            get { return _PubTel; }
            set
            {
                if(_PubTel!=value){
                    _PubTel=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PubTel");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CharIndex;
        public string CharIndex
        {
            get { return _CharIndex; }
            set
            {
                if(_CharIndex!=value){
                    _CharIndex=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CharIndex");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Note;
        public string Note
        {
            get { return _Note; }
            set
            {
                if(_Note!=value){
                    _Note=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Note");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelB_BookPublish, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_B_BookAuthor table in the BookEShop Database.
    /// </summary>
    public partial class ModelB_BookAuthor: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelB_BookAuthor> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelB_BookAuthor>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelB_BookAuthor> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelB_BookAuthor item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelB_BookAuthor item=new ModelB_BookAuthor();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelB_BookAuthor> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelB_BookAuthor(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelB_BookAuthor.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookAuthor>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelB_BookAuthor(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelB_BookAuthor(Expression<Func<ModelB_BookAuthor, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelB_BookAuthor> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelB_BookAuthor> _repo;
            
            if(db.TestMode){
                ModelB_BookAuthor.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookAuthor>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelB_BookAuthor> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelB_BookAuthor SingleOrDefault(Expression<Func<ModelB_BookAuthor, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelB_BookAuthor single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelB_BookAuthor SingleOrDefault(Expression<Func<ModelB_BookAuthor, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelB_BookAuthor single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelB_BookAuthor, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelB_BookAuthor, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelB_BookAuthor> Find(Expression<Func<ModelB_BookAuthor, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelB_BookAuthor> Find(Expression<Func<ModelB_BookAuthor, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelB_BookAuthor> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelB_BookAuthor> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelB_BookAuthor> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelB_BookAuthor> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelB_BookAuthor> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelB_BookAuthor> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Name.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelB_BookAuthor)){
                ModelB_BookAuthor compare=(ModelB_BookAuthor)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.Name.ToString();
                    }

        public string DescriptorColumn() {
            return "Name";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Name";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if(_Name!=value){
                    _Name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Photo;
        public string Photo
        {
            get { return _Photo; }
            set
            {
                if(_Photo!=value){
                    _Photo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Photo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Note;
        public string Note
        {
            get { return _Note; }
            set
            {
                if(_Note!=value){
                    _Note=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Note");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelB_BookAuthor, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Blocker table in the BookEShop Database.
    /// </summary>
    public partial class ModelBlocker: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelBlocker> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelBlocker>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelBlocker> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelBlocker item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelBlocker item=new ModelBlocker();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelBlocker> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelBlocker(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelBlocker.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelBlocker>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelBlocker(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelBlocker(Expression<Func<ModelBlocker, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelBlocker> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelBlocker> _repo;
            
            if(db.TestMode){
                ModelBlocker.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelBlocker>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelBlocker> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelBlocker SingleOrDefault(Expression<Func<ModelBlocker, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelBlocker single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelBlocker SingleOrDefault(Expression<Func<ModelBlocker, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelBlocker single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelBlocker, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelBlocker, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelBlocker> Find(Expression<Func<ModelBlocker, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelBlocker> Find(Expression<Func<ModelBlocker, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelBlocker> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelBlocker> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelBlocker> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelBlocker> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelBlocker> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelBlocker> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.BlockerType.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelBlocker)){
                ModelBlocker compare=(ModelBlocker)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.BlockerType.ToString();
                    }

        public string DescriptorColumn() {
            return "BlockerType";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "BlockerType";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BlockerType;
        public string BlockerType
        {
            get { return _BlockerType; }
            set
            {
                if(_BlockerType!=value){
                    _BlockerType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BlockerType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BlockerName;
        public string BlockerName
        {
            get { return _BlockerName; }
            set
            {
                if(_BlockerName!=value){
                    _BlockerName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BlockerName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BlockerID;
        public string BlockerID
        {
            get { return _BlockerID; }
            set
            {
                if(_BlockerID!=value){
                    _BlockerID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BlockerID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BlockerContent;
        public string BlockerContent
        {
            get { return _BlockerContent; }
            set
            {
                if(_BlockerContent!=value){
                    _BlockerContent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BlockerContent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BlockerDesc;
        public string BlockerDesc
        {
            get { return _BlockerDesc; }
            set
            {
                if(_BlockerDesc!=value){
                    _BlockerDesc=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BlockerDesc");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _UpdateTime;
        public DateTime? UpdateTime
        {
            get { return _UpdateTime; }
            set
            {
                if(_UpdateTime!=value){
                    _UpdateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UpdateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _BlockerPage;
        public string BlockerPage
        {
            get { return _BlockerPage; }
            set
            {
                if(_BlockerPage!=value){
                    _BlockerPage=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BlockerPage");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelBlocker, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Config table in the BookEShop Database.
    /// </summary>
    public partial class ModelConfig: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelConfig> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelConfig>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelConfig> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelConfig item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelConfig item=new ModelConfig();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelConfig> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelConfig(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelConfig.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelConfig>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelConfig(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelConfig(Expression<Func<ModelConfig, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelConfig> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelConfig> _repo;
            
            if(db.TestMode){
                ModelConfig.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelConfig>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelConfig> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelConfig SingleOrDefault(Expression<Func<ModelConfig, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelConfig single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelConfig SingleOrDefault(Expression<Func<ModelConfig, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelConfig single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelConfig, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelConfig, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelConfig> Find(Expression<Func<ModelConfig, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelConfig> Find(Expression<Func<ModelConfig, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelConfig> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelConfig> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelConfig> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelConfig> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelConfig> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelConfig> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.SiteTitle.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelConfig)){
                ModelConfig compare=(ModelConfig)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.SiteTitle.ToString();
                    }

        public string DescriptorColumn() {
            return "SiteTitle";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "SiteTitle";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SiteTitle;
        public string SiteTitle
        {
            get { return _SiteTitle; }
            set
            {
                if(_SiteTitle!=value){
                    _SiteTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SiteUrl;
        public string SiteUrl
        {
            get { return _SiteUrl; }
            set
            {
                if(_SiteUrl!=value){
                    _SiteUrl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteUrl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SiteLogo;
        public string SiteLogo
        {
            get { return _SiteLogo; }
            set
            {
                if(_SiteLogo!=value){
                    _SiteLogo=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteLogo");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _WebmasterName;
        public string WebmasterName
        {
            get { return _WebmasterName; }
            set
            {
                if(_WebmasterName!=value){
                    _WebmasterName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="WebmasterName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _WebmasterEmail;
        public string WebmasterEmail
        {
            get { return _WebmasterEmail; }
            set
            {
                if(_WebmasterEmail!=value){
                    _WebmasterEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="WebmasterEmail");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SiteCopyright;
        public string SiteCopyright
        {
            get { return _SiteCopyright; }
            set
            {
                if(_SiteCopyright!=value){
                    _SiteCopyright=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteCopyright");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SiteDescription;
        public string SiteDescription
        {
            get { return _SiteDescription; }
            set
            {
                if(_SiteDescription!=value){
                    _SiteDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SiteKeywords;
        public string SiteKeywords
        {
            get { return _SiteKeywords; }
            set
            {
                if(_SiteKeywords!=value){
                    _SiteKeywords=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteKeywords");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SiteState;
        public int? SiteState
        {
            get { return _SiteState; }
            set
            {
                if(_SiteState!=value){
                    _SiteState=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteState");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _SiteCloseText;
        public string SiteCloseText
        {
            get { return _SiteCloseText; }
            set
            {
                if(_SiteCloseText!=value){
                    _SiteCloseText=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteCloseText");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _MailObject;
        public int? MailObject
        {
            get { return _MailObject; }
            set
            {
                if(_MailObject!=value){
                    _MailObject=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailObject");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailServer;
        public string MailServer
        {
            get { return _MailServer; }
            set
            {
                if(_MailServer!=value){
                    _MailServer=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailServer");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailServerUserName;
        public string MailServerUserName
        {
            get { return _MailServerUserName; }
            set
            {
                if(_MailServerUserName!=value){
                    _MailServerUserName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailServerUserName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailServerPassWord;
        public string MailServerPassWord
        {
            get { return _MailServerPassWord; }
            set
            {
                if(_MailServerPassWord!=value){
                    _MailServerPassWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailServerPassWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailDomain;
        public string MailDomain
        {
            get { return _MailDomain; }
            set
            {
                if(_MailDomain!=value){
                    _MailDomain=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailDomain");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _TemplatePath;
        public string TemplatePath
        {
            get { return _TemplatePath; }
            set
            {
                if(_TemplatePath!=value){
                    _TemplatePath=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TemplatePath");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UrlRewrite;
        public string UrlRewrite
        {
            get { return _UrlRewrite; }
            set
            {
                if(_UrlRewrite!=value){
                    _UrlRewrite=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UrlRewrite");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UploadType;
        public string UploadType
        {
            get { return _UploadType; }
            set
            {
                if(_UploadType!=value){
                    _UploadType=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UploadType");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UploadMwidth;
        public string UploadMwidth
        {
            get { return _UploadMwidth; }
            set
            {
                if(_UploadMwidth!=value){
                    _UploadMwidth=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UploadMwidth");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _UploadSwidth;
        public string UploadSwidth
        {
            get { return _UploadSwidth; }
            set
            {
                if(_UploadSwidth!=value){
                    _UploadSwidth=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UploadSwidth");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _SiteShow;
        public int? SiteShow
        {
            get { return _SiteShow; }
            set
            {
                if(_SiteShow!=value){
                    _SiteShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SiteShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _TemplateDir;
        public string TemplateDir
        {
            get { return _TemplateDir; }
            set
            {
                if(_TemplateDir!=value){
                    _TemplateDir=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TemplateDir");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelConfig, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_ArticleClass table in the BookEShop Database.
    /// </summary>
    public partial class ModelArticleClass: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelArticleClass> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelArticleClass>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelArticleClass> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelArticleClass item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelArticleClass item=new ModelArticleClass();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelArticleClass> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelArticleClass(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelArticleClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelArticleClass>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelArticleClass(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelArticleClass(Expression<Func<ModelArticleClass, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelArticleClass> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelArticleClass> _repo;
            
            if(db.TestMode){
                ModelArticleClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelArticleClass>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelArticleClass> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelArticleClass SingleOrDefault(Expression<Func<ModelArticleClass, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelArticleClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelArticleClass SingleOrDefault(Expression<Func<ModelArticleClass, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelArticleClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelArticleClass, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelArticleClass, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelArticleClass> Find(Expression<Func<ModelArticleClass, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelArticleClass> Find(Expression<Func<ModelArticleClass, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelArticleClass> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelArticleClass> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelArticleClass> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelArticleClass> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelArticleClass> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelArticleClass> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ClassID";
        }

        public object KeyValue()
        {
            return this.ClassID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ClassCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelArticleClass)){
                ModelArticleClass compare=(ModelArticleClass)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ClassID;
        }
        
        public string DescriptorValue()
        {
                            return this.ClassCode.ToString();
                    }

        public string DescriptorColumn() {
            return "ClassCode";
        }
        public static string GetKeyColumn()
        {
            return "ClassID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ClassCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ClassID;
        public int ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ChannelID;
        public int? ChannelID
        {
            get { return _ChannelID; }
            set
            {
                if(_ChannelID!=value){
                    _ChannelID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ChannelID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassCode;
        public string ClassCode
        {
            get { return _ClassCode; }
            set
            {
                if(_ClassCode!=value){
                    _ClassCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassPhoto;
        public string ClassPhoto
        {
            get { return _ClassPhoto; }
            set
            {
                if(_ClassPhoto!=value){
                    _ClassPhoto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassPhoto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassKeyWord;
        public string ClassKeyWord
        {
            get { return _ClassKeyWord; }
            set
            {
                if(_ClassKeyWord!=value){
                    _ClassKeyWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassKeyWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassDescription;
        public string ClassDescription
        {
            get { return _ClassDescription; }
            set
            {
                if(_ClassDescription!=value){
                    _ClassDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassTitle;
        public string ClassTitle
        {
            get { return _ClassTitle; }
            set
            {
                if(_ClassTitle!=value){
                    _ClassTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ParentID;
        public int? ParentID
        {
            get { return _ParentID; }
            set
            {
                if(_ParentID!=value){
                    _ParentID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Info;
        public string Info
        {
            get { return _Info; }
            set
            {
                if(_Info!=value){
                    _Info=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Info");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelArticleClass, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Article table in the BookEShop Database.
    /// </summary>
    public partial class ModelArticle: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelArticle> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelArticle>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelArticle> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelArticle item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelArticle item=new ModelArticle();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelArticle> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelArticle(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelArticle.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelArticle>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelArticle(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelArticle(Expression<Func<ModelArticle, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelArticle> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelArticle> _repo;
            
            if(db.TestMode){
                ModelArticle.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelArticle>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelArticle> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelArticle SingleOrDefault(Expression<Func<ModelArticle, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelArticle single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelArticle SingleOrDefault(Expression<Func<ModelArticle, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelArticle single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelArticle, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelArticle, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelArticle> Find(Expression<Func<ModelArticle, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelArticle> Find(Expression<Func<ModelArticle, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelArticle> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelArticle> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelArticle> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelArticle> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelArticle> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelArticle> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Title.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelArticle)){
                ModelArticle compare=(ModelArticle)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.Title.ToString();
                    }

        public string DescriptorColumn() {
            return "Title";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "Title";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ChannelID;
        public int? ChannelID
        {
            get { return _ChannelID; }
            set
            {
                if(_ChannelID!=value){
                    _ChannelID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ChannelID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                if(_Title!=value){
                    _Title=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Title");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _KeyWords;
        public string KeyWords
        {
            get { return _KeyWords; }
            set
            {
                if(_KeyWords!=value){
                    _KeyWords=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="KeyWords");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ClassID;
        public int? ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CopyFrom;
        public string CopyFrom
        {
            get { return _CopyFrom; }
            set
            {
                if(_CopyFrom!=value){
                    _CopyFrom=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CopyFrom");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Author;
        public string Author
        {
            get { return _Author; }
            set
            {
                if(_Author!=value){
                    _Author=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Author");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Editor;
        public string Editor
        {
            get { return _Editor; }
            set
            {
                if(_Editor!=value){
                    _Editor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Editor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Content;
        public string Content
        {
            get { return _Content; }
            set
            {
                if(_Content!=value){
                    _Content=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Content");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ShortContent;
        public string ShortContent
        {
            get { return _ShortContent; }
            set
            {
                if(_ShortContent!=value){
                    _ShortContent=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShortContent");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsUrl;
        public int? IsUrl
        {
            get { return _IsUrl; }
            set
            {
                if(_IsUrl!=value){
                    _IsUrl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsUrl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LinkUrl;
        public string LinkUrl
        {
            get { return _LinkUrl; }
            set
            {
                if(_LinkUrl!=value){
                    _LinkUrl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LinkUrl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _DefaultPicUrl;
        public string DefaultPicUrl
        {
            get { return _DefaultPicUrl; }
            set
            {
                if(_DefaultPicUrl!=value){
                    _DefaultPicUrl=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="DefaultPicUrl");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Hits;
        public int? Hits
        {
            get { return _Hits; }
            set
            {
                if(_Hits!=value){
                    _Hits=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Hits");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _CutomField;
        public string CutomField
        {
            get { return _CutomField; }
            set
            {
                if(_CutomField!=value){
                    _CutomField=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CutomField");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Passed;
        public int? Passed
        {
            get { return _Passed; }
            set
            {
                if(_Passed!=value){
                    _Passed=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Passed");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _AddDateTime;
        public DateTime? AddDateTime
        {
            get { return _AddDateTime; }
            set
            {
                if(_AddDateTime!=value){
                    _AddDateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AddDateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _UpDateTime;
        public DateTime? UpDateTime
        {
            get { return _UpDateTime; }
            set
            {
                if(_UpDateTime!=value){
                    _UpDateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UpDateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelArticle, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Admin table in the BookEShop Database.
    /// </summary>
    public partial class ModelAdmin: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelAdmin> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelAdmin>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelAdmin> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelAdmin item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelAdmin item=new ModelAdmin();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelAdmin> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelAdmin(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelAdmin.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelAdmin>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelAdmin(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelAdmin(Expression<Func<ModelAdmin, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelAdmin> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelAdmin> _repo;
            
            if(db.TestMode){
                ModelAdmin.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelAdmin>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelAdmin> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelAdmin SingleOrDefault(Expression<Func<ModelAdmin, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelAdmin single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelAdmin SingleOrDefault(Expression<Func<ModelAdmin, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelAdmin single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelAdmin, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelAdmin, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelAdmin> Find(Expression<Func<ModelAdmin, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelAdmin> Find(Expression<Func<ModelAdmin, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelAdmin> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelAdmin> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelAdmin> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelAdmin> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelAdmin> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelAdmin> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ID";
        }

        public object KeyValue()
        {
            return this.ID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.AdminName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelAdmin)){
                ModelAdmin compare=(ModelAdmin)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ID;
        }
        
        public string DescriptorValue()
        {
                            return this.AdminName.ToString();
                    }

        public string DescriptorColumn() {
            return "AdminName";
        }
        public static string GetKeyColumn()
        {
            return "ID";
        }        
        public static string GetDescriptorColumn()
        {
            return "AdminName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ID;
        public int ID
        {
            get { return _ID; }
            set
            {
                if(_ID!=value){
                    _ID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _AdminName;
        public string AdminName
        {
            get { return _AdminName; }
            set
            {
                if(_AdminName!=value){
                    _AdminName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AdminName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _AdminPwd;
        public string AdminPwd
        {
            get { return _AdminPwd; }
            set
            {
                if(_AdminPwd!=value){
                    _AdminPwd=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="AdminPwd");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _LoginTimes;
        public int? LoginTimes
        {
            get { return _LoginTimes; }
            set
            {
                if(_LoginTimes!=value){
                    _LoginTimes=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LoginTimes");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _LoginTime;
        public DateTime? LoginTime
        {
            get { return _LoginTime; }
            set
            {
                if(_LoginTime!=value){
                    _LoginTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LoginTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _LoginIP;
        public string LoginIP
        {
            get { return _LoginIP; }
            set
            {
                if(_LoginIP!=value){
                    _LoginIP=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LoginIP");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelAdmin, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_B_BookIsbn table in the BookEShop Database.
    /// </summary>
    public partial class ModelB_BookIsbn: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelB_BookIsbn> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelB_BookIsbn>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelB_BookIsbn> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelB_BookIsbn item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelB_BookIsbn item=new ModelB_BookIsbn();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelB_BookIsbn> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelB_BookIsbn(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelB_BookIsbn.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookIsbn>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelB_BookIsbn(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelB_BookIsbn(Expression<Func<ModelB_BookIsbn, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelB_BookIsbn> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelB_BookIsbn> _repo;
            
            if(db.TestMode){
                ModelB_BookIsbn.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelB_BookIsbn>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelB_BookIsbn> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelB_BookIsbn SingleOrDefault(Expression<Func<ModelB_BookIsbn, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelB_BookIsbn single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelB_BookIsbn SingleOrDefault(Expression<Func<ModelB_BookIsbn, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelB_BookIsbn single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelB_BookIsbn, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelB_BookIsbn, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelB_BookIsbn> Find(Expression<Func<ModelB_BookIsbn, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelB_BookIsbn> Find(Expression<Func<ModelB_BookIsbn, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelB_BookIsbn> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelB_BookIsbn> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelB_BookIsbn> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelB_BookIsbn> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelB_BookIsbn> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelB_BookIsbn> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.IsbnKind.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelB_BookIsbn)){
                ModelB_BookIsbn compare=(ModelB_BookIsbn)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.IsbnKind.ToString();
                    }

        public string DescriptorColumn() {
            return "IsbnKind";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "IsbnKind";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _IsbnKind;
        public string IsbnKind
        {
            get { return _IsbnKind; }
            set
            {
                if(_IsbnKind!=value){
                    _IsbnKind=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsbnKind");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Note;
        public string Note
        {
            get { return _Note; }
            set
            {
                if(_Note!=value){
                    _Note=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Note");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _CreateTime;
        public DateTime? CreateTime
        {
            get { return _CreateTime; }
            set
            {
                if(_CreateTime!=value){
                    _CreateTime=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreateTime");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelB_BookIsbn, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_FriendClass table in the BookEShop Database.
    /// </summary>
    public partial class ModelFriendClass: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelFriendClass> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelFriendClass>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelFriendClass> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelFriendClass item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelFriendClass item=new ModelFriendClass();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelFriendClass> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelFriendClass(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelFriendClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelFriendClass>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelFriendClass(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelFriendClass(Expression<Func<ModelFriendClass, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelFriendClass> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelFriendClass> _repo;
            
            if(db.TestMode){
                ModelFriendClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelFriendClass>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelFriendClass> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelFriendClass SingleOrDefault(Expression<Func<ModelFriendClass, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelFriendClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelFriendClass SingleOrDefault(Expression<Func<ModelFriendClass, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelFriendClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelFriendClass, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelFriendClass, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelFriendClass> Find(Expression<Func<ModelFriendClass, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelFriendClass> Find(Expression<Func<ModelFriendClass, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelFriendClass> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelFriendClass> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelFriendClass> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelFriendClass> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelFriendClass> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelFriendClass> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ClassID";
        }

        public object KeyValue()
        {
            return this.ClassID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ClassCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelFriendClass)){
                ModelFriendClass compare=(ModelFriendClass)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ClassID;
        }
        
        public string DescriptorValue()
        {
                            return this.ClassCode.ToString();
                    }

        public string DescriptorColumn() {
            return "ClassCode";
        }
        public static string GetKeyColumn()
        {
            return "ClassID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ClassCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ClassID;
        public int ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ChannelID;
        public int? ChannelID
        {
            get { return _ChannelID; }
            set
            {
                if(_ChannelID!=value){
                    _ChannelID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ChannelID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassCode;
        public string ClassCode
        {
            get { return _ClassCode; }
            set
            {
                if(_ClassCode!=value){
                    _ClassCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassPhoto;
        public string ClassPhoto
        {
            get { return _ClassPhoto; }
            set
            {
                if(_ClassPhoto!=value){
                    _ClassPhoto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassPhoto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassKeyWord;
        public string ClassKeyWord
        {
            get { return _ClassKeyWord; }
            set
            {
                if(_ClassKeyWord!=value){
                    _ClassKeyWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassKeyWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassDescription;
        public string ClassDescription
        {
            get { return _ClassDescription; }
            set
            {
                if(_ClassDescription!=value){
                    _ClassDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassTitle;
        public string ClassTitle
        {
            get { return _ClassTitle; }
            set
            {
                if(_ClassTitle!=value){
                    _ClassTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ParentID;
        public int? ParentID
        {
            get { return _ParentID; }
            set
            {
                if(_ParentID!=value){
                    _ParentID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Info;
        public string Info
        {
            get { return _Info; }
            set
            {
                if(_Info!=value){
                    _Info=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Info");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelFriendClass, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_Shop_Area table in the BookEShop Database.
    /// </summary>
    public partial class ModelShop_Area: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelShop_Area> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelShop_Area>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelShop_Area> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelShop_Area item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelShop_Area item=new ModelShop_Area();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelShop_Area> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelShop_Area(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelShop_Area.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Area>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelShop_Area(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelShop_Area(Expression<Func<ModelShop_Area, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelShop_Area> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelShop_Area> _repo;
            
            if(db.TestMode){
                ModelShop_Area.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelShop_Area>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelShop_Area> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelShop_Area SingleOrDefault(Expression<Func<ModelShop_Area, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelShop_Area single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelShop_Area SingleOrDefault(Expression<Func<ModelShop_Area, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelShop_Area single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelShop_Area, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelShop_Area, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelShop_Area> Find(Expression<Func<ModelShop_Area, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelShop_Area> Find(Expression<Func<ModelShop_Area, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelShop_Area> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelShop_Area> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelShop_Area> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelShop_Area> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelShop_Area> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelShop_Area> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Area_Id";
        }

        public object KeyValue()
        {
            return this.Area_Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<string>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Area_Id.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelShop_Area)){
                ModelShop_Area compare=(ModelShop_Area)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        public string DescriptorValue()
        {
                            return this.Area_Id.ToString();
                    }

        public string DescriptorColumn() {
            return "Area_Id";
        }
        public static string GetKeyColumn()
        {
            return "Area_Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "Area_Id";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        string _Area_Id;
        public string Area_Id
        {
            get { return _Area_Id; }
            set
            {
                if(_Area_Id!=value){
                    _Area_Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Area_Id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Area_name;
        public string Area_name
        {
            get { return _Area_name; }
            set
            {
                if(_Area_name!=value){
                    _Area_name=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Area_name");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _Area_Level;
        public int? Area_Level
        {
            get { return _Area_Level; }
            set
            {
                if(_Area_Level!=value){
                    _Area_Level=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Area_Level");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Area_Parent_Id;
        public string Area_Parent_Id
        {
            get { return _Area_Parent_Id; }
            set
            {
                if(_Area_Parent_Id!=value){
                    _Area_Parent_Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Area_Parent_Id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelShop_Area, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_HotKeyWordClass table in the BookEShop Database.
    /// </summary>
    public partial class ModelHotKeyWordClass: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelHotKeyWordClass> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelHotKeyWordClass>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelHotKeyWordClass> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelHotKeyWordClass item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelHotKeyWordClass item=new ModelHotKeyWordClass();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelHotKeyWordClass> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelHotKeyWordClass(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelHotKeyWordClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelHotKeyWordClass>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelHotKeyWordClass(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelHotKeyWordClass(Expression<Func<ModelHotKeyWordClass, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelHotKeyWordClass> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelHotKeyWordClass> _repo;
            
            if(db.TestMode){
                ModelHotKeyWordClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelHotKeyWordClass>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelHotKeyWordClass> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelHotKeyWordClass SingleOrDefault(Expression<Func<ModelHotKeyWordClass, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelHotKeyWordClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelHotKeyWordClass SingleOrDefault(Expression<Func<ModelHotKeyWordClass, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelHotKeyWordClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelHotKeyWordClass, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelHotKeyWordClass, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelHotKeyWordClass> Find(Expression<Func<ModelHotKeyWordClass, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelHotKeyWordClass> Find(Expression<Func<ModelHotKeyWordClass, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelHotKeyWordClass> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelHotKeyWordClass> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelHotKeyWordClass> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelHotKeyWordClass> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelHotKeyWordClass> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelHotKeyWordClass> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ClassID";
        }

        public object KeyValue()
        {
            return this.ClassID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ClassCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelHotKeyWordClass)){
                ModelHotKeyWordClass compare=(ModelHotKeyWordClass)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ClassID;
        }
        
        public string DescriptorValue()
        {
                            return this.ClassCode.ToString();
                    }

        public string DescriptorColumn() {
            return "ClassCode";
        }
        public static string GetKeyColumn()
        {
            return "ClassID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ClassCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ClassID;
        public int ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ChannelID;
        public int? ChannelID
        {
            get { return _ChannelID; }
            set
            {
                if(_ChannelID!=value){
                    _ChannelID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ChannelID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassCode;
        public string ClassCode
        {
            get { return _ClassCode; }
            set
            {
                if(_ClassCode!=value){
                    _ClassCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassPhoto;
        public string ClassPhoto
        {
            get { return _ClassPhoto; }
            set
            {
                if(_ClassPhoto!=value){
                    _ClassPhoto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassPhoto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassKeyWord;
        public string ClassKeyWord
        {
            get { return _ClassKeyWord; }
            set
            {
                if(_ClassKeyWord!=value){
                    _ClassKeyWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassKeyWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassDescription;
        public string ClassDescription
        {
            get { return _ClassDescription; }
            set
            {
                if(_ClassDescription!=value){
                    _ClassDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassTitle;
        public string ClassTitle
        {
            get { return _ClassTitle; }
            set
            {
                if(_ClassTitle!=value){
                    _ClassTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ParentID;
        public int? ParentID
        {
            get { return _ParentID; }
            set
            {
                if(_ParentID!=value){
                    _ParentID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Info;
        public string Info
        {
            get { return _Info; }
            set
            {
                if(_Info!=value){
                    _Info=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Info");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelHotKeyWordClass, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_AdminRole table in the BookEShop Database.
    /// </summary>
    public partial class ModelAdminRole: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelAdminRole> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelAdminRole>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelAdminRole> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelAdminRole item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelAdminRole item=new ModelAdminRole();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelAdminRole> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelAdminRole(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelAdminRole.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelAdminRole>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelAdminRole(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelAdminRole(Expression<Func<ModelAdminRole, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelAdminRole> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelAdminRole> _repo;
            
            if(db.TestMode){
                ModelAdminRole.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelAdminRole>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelAdminRole> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelAdminRole SingleOrDefault(Expression<Func<ModelAdminRole, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelAdminRole single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelAdminRole SingleOrDefault(Expression<Func<ModelAdminRole, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelAdminRole single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelAdminRole, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelAdminRole, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelAdminRole> Find(Expression<Func<ModelAdminRole, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelAdminRole> Find(Expression<Func<ModelAdminRole, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelAdminRole> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelAdminRole> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelAdminRole> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelAdminRole> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelAdminRole> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelAdminRole> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "Id";
        }

        public object KeyValue()
        {
            return this.Id;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.RoleName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelAdminRole)){
                ModelAdminRole compare=(ModelAdminRole)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.Id;
        }
        
        public string DescriptorValue()
        {
                            return this.RoleName.ToString();
                    }

        public string DescriptorColumn() {
            return "RoleName";
        }
        public static string GetKeyColumn()
        {
            return "Id";
        }        
        public static string GetDescriptorColumn()
        {
            return "RoleName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _Id;
        public int Id
        {
            get { return _Id; }
            set
            {
                if(_Id!=value){
                    _Id=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Id");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _RoleName;
        public string RoleName
        {
            get { return _RoleName; }
            set
            {
                if(_RoleName!=value){
                    _RoleName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RoleName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelAdminRole, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Tbl_ArticleTopicClass table in the BookEShop Database.
    /// </summary>
    public partial class ModelArticleTopicClass: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<ModelArticleTopicClass> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<ModelArticleTopicClass>(new V5_EShop.Data.BookEShopDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<ModelArticleTopicClass> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(ModelArticleTopicClass item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                ModelArticleTopicClass item=new ModelArticleTopicClass();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<ModelArticleTopicClass> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        V5_EShop.Data.BookEShopDB _db;
        public ModelArticleTopicClass(string connectionString, string providerName) {

            _db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                ModelArticleTopicClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelArticleTopicClass>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public ModelArticleTopicClass(){
             _db=new V5_EShop.Data.BookEShopDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public ModelArticleTopicClass(Expression<Func<ModelArticleTopicClass, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<ModelArticleTopicClass> GetRepo(string connectionString, string providerName){
            V5_EShop.Data.BookEShopDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new V5_EShop.Data.BookEShopDB();
            }else{
                db=new V5_EShop.Data.BookEShopDB(connectionString, providerName);
            }
            IRepository<ModelArticleTopicClass> _repo;
            
            if(db.TestMode){
                ModelArticleTopicClass.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<ModelArticleTopicClass>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<ModelArticleTopicClass> GetRepo(){
            return GetRepo("","");
        }
        
        public static ModelArticleTopicClass SingleOrDefault(Expression<Func<ModelArticleTopicClass, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            ModelArticleTopicClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static ModelArticleTopicClass SingleOrDefault(Expression<Func<ModelArticleTopicClass, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            ModelArticleTopicClass single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<ModelArticleTopicClass, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<ModelArticleTopicClass, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<ModelArticleTopicClass> Find(Expression<Func<ModelArticleTopicClass, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<ModelArticleTopicClass> Find(Expression<Func<ModelArticleTopicClass, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<ModelArticleTopicClass> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<ModelArticleTopicClass> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<ModelArticleTopicClass> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<ModelArticleTopicClass> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<ModelArticleTopicClass> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<ModelArticleTopicClass> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ClassID";
        }

        public object KeyValue()
        {
            return this.ClassID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.ClassCode.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(ModelArticleTopicClass)){
                ModelArticleTopicClass compare=(ModelArticleTopicClass)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ClassID;
        }
        
        public string DescriptorValue()
        {
                            return this.ClassCode.ToString();
                    }

        public string DescriptorColumn() {
            return "ClassCode";
        }
        public static string GetKeyColumn()
        {
            return "ClassID";
        }        
        public static string GetDescriptorColumn()
        {
            return "ClassCode";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ClassID;
        public int ClassID
        {
            get { return _ClassID; }
            set
            {
                if(_ClassID!=value){
                    _ClassID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ChannelID;
        public int? ChannelID
        {
            get { return _ChannelID; }
            set
            {
                if(_ChannelID!=value){
                    _ChannelID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ChannelID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassCode;
        public string ClassCode
        {
            get { return _ClassCode; }
            set
            {
                if(_ClassCode!=value){
                    _ClassCode=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassCode");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassPhoto;
        public string ClassPhoto
        {
            get { return _ClassPhoto; }
            set
            {
                if(_ClassPhoto!=value){
                    _ClassPhoto=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassPhoto");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassKeyWord;
        public string ClassKeyWord
        {
            get { return _ClassKeyWord; }
            set
            {
                if(_ClassKeyWord!=value){
                    _ClassKeyWord=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassKeyWord");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassDescription;
        public string ClassDescription
        {
            get { return _ClassDescription; }
            set
            {
                if(_ClassDescription!=value){
                    _ClassDescription=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassDescription");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _ClassTitle;
        public string ClassTitle
        {
            get { return _ClassTitle; }
            set
            {
                if(_ClassTitle!=value){
                    _ClassTitle=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ClassTitle");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ParentID;
        public int? ParentID
        {
            get { return _ParentID; }
            set
            {
                if(_ParentID!=value){
                    _ParentID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ParentID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Info;
        public string Info
        {
            get { return _Info; }
            set
            {
                if(_Info!=value){
                    _Info=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Info");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _IsShow;
        public int? IsShow
        {
            get { return _IsShow; }
            set
            {
                if(_IsShow!=value){
                    _IsShow=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="IsShow");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<ModelArticleTopicClass, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
}
